# -*- coding: utf-8 -*-

import frappe
import json


@frappe.whitelist()
def get_doc_withmeta(doc_type, doc_name):
    count = 1001
    data = {
        "docElements": [
            {
                "elementType": "text",
                "id": 1117,
                "containerId": "0_header",
                "x": 10,
                "y": 30,
                "width": 220,
                "height": 20,
                "content": "单据编号：${name}",
                "richText": False,
                "richTextContent": None,
                "richTextHtml": "",
                "eval": False,
                "styleId": "",
                "bold": False,
                "italic": False,
                "underline": False,
                "strikethrough": False,
                "horizontalAlignment": "left",
                "verticalAlignment": "top",
                "textColor": "#000000",
                "backgroundColor": "",
                "font": "simkai",
                "fontSize": 12,
                "lineSpacing": 1,
                "borderColor": "#000000",
                "borderWidth": 1,
                "borderAll": False,
                "borderLeft": False,
                "borderTop": False,
                "borderRight": False,
                "borderBottom": False,
                "paddingLeft": 2,
                "paddingTop": 2,
                "paddingRight": 2,
                "paddingBottom": 2,
                "printIf": "",
                "removeEmptyElement": False,
                "alwaysPrintOnSamePage": True,
                "pattern": "",
                "link": "",
                "cs_condition": "",
                "cs_styleId": "",
                "cs_additionalRules": "",
                "cs_bold": False,
                "cs_italic": False,
                "cs_underline": False,
                "cs_strikethrough": False,
                "cs_horizontalAlignment": "left",
                "cs_verticalAlignment": "top",
                "cs_textColor": "#000000",
                "cs_backgroundColor": "",
                "cs_font": "helvetica",
                "cs_fontSize": 12,
                "cs_lineSpacing": 1,
                "cs_borderColor": "#000000",
                "cs_borderWidth": "1",
                "cs_borderAll": False,
                "cs_borderLeft": False,
                "cs_borderTop": False,
                "cs_borderRight": False,
                "cs_borderBottom": False,
                "cs_paddingLeft": 2,
                "cs_paddingTop": 2,
                "cs_paddingRight": 2,
                "cs_paddingBottom": 2,
                "spreadsheet_hide": False,
                "spreadsheet_column": "",
                "spreadsheet_colspan": "",
                "spreadsheet_addEmptyRow": False,
                "spreadsheet_type": "",
                "spreadsheet_pattern": "",
                "spreadsheet_textWrap": False,
            }
        ],
        "parameters": [
            {
                "id": 1,
                "name": "page_count",
                "type": "number",
                "eval": False,
                "pattern": "",
                "expression": "",
                "showOnlyNameType": False,
                "testData": "",
            },
            {
                "id": 2,
                "name": "page_number",
                "type": "number",
                "eval": False,
                "pattern": "",
                "expression": "",
                "showOnlyNameType": False,
                "testData": "",
            },
            {
                "id": 3,
                "name": "name",
                "type": "string",
                "eval": False,
                "pattern": "",
                "expression": "",
                "showOnlyNameType": False,
                "testData": doc_name,
            },
        ],
        "styles": [],
        "version": 1,
        "documentProperties": {
            "pageFormat": "A4",
            "pageWidth": "",
            "pageHeight": "",
            "unit": "mm",
            "orientation": "portrait",
            "contentHeight": "",
            "marginLeft": "20",
            "marginTop": "20",
            "marginRight": "20",
            "marginBottom": "10",
            "header": True,
            "headerSize": "60",
            "headerDisplay": "always",
            "footer": True,
            "footerSize": "60",
            "footerDisplay": "always",
            "patternLocale": "en",
            "patternCurrencySymbol": "€",
        },
    }

    doc = frappe.get_doc(doc_type, doc_name)
    skip_field_types = [
        "Section Break",
        "Column Break",
        "Tab Break",
        "update_auto_repeat_reference",
        "Button",
        "Table",
    ]
    for item in doc.meta.fields:
        if item.fieldtype not in skip_field_types:
            item.fieldtype
            new_item = {
                "id": count,
                "name": item.fieldname,
                "type": get_type_map(item.fieldtype),
                "eval": False,
                "pattern": "",
                "expression": "",
                "showOnlyNameType": False,
                "testData": getattr(doc, item.fieldname),
            }
            data["parameters"].append(new_item)
            count += 1
        if item.fieldtype == "Table":
            lines = getattr(doc, item.fieldname)
            lines_json = frappe.as_json(lines)
            # print(lines_json)
            new_item = {
                "id": count,
                "name": item.fieldname,
                "type": "array",
                "eval": False,
                "pattern": "",
                "expression": "",
                "showOnlyNameType": False,
                "testData": lines_json,
                "children": [],
            }
            count += 1
            child_meta = frappe.get_meta(item.options)
            for sub_item in child_meta.fields:
                if sub_item.fieldtype not in skip_field_types:
                    new_sub_item = {
                        "id": count,
                        "name": sub_item.fieldname,
                        "type": get_type_map(sub_item.fieldtype),
                        "eval": False,
                        "pattern": "",
                        "expression": "",
                        "showOnlyNameType": False,
                    }
                    count += 1
                    new_item["children"].append(new_sub_item)
            data["parameters"].append(new_item)
    return data


def get_type_map(data_type):
    if data_type == "Data":
        return "string"
    elif data_type == "Link" or data_type == "Dynamic Link":
        return "string"
    elif data_type == "Check":
        return "boolean"
    elif data_type == "Select":
        return "string"
    elif data_type == "Table":
        return "array"
    elif data_type == "Attach" or data_type == "Attach Image":
        return "string"
    elif data_type == "Text Editor":
        return "rich_text"
    elif data_type == "Date":
        return "date"
    elif data_type == "Date and Time":
        return "string"
    elif data_type == "Barcode":
        return "string"
    elif data_type == "Code":
        return "rich_text"
    elif data_type == "Color":
        return "string"
    elif data_type == "Currency":
        return "number"
    elif data_type == "Float":
        return "number"
    elif data_type == "Geolocation":
        return "string"
    elif data_type == "HTML":
        return "rich_text"
    elif data_type == "Image":
        return "string"
    elif data_type == "Int":
        return "number"
    elif (
        data_type == "Small Text"
        or data_type == "Long Text"
        or data_type == "Text"
        or data_type == "Markdown Editor"
    ):
        return "string"
    elif data_type == "Password":
        return "string"
    elif data_type == "Percent":
        return "number"
    elif data_type == "Rating":
        return "number"
    elif data_type == "Signature":
        return "string"
    elif data_type == "Table MultiSelect":
        return "string"
    elif data_type == "Time":
        return "string"
    elif data_type == "Duration":
        return "string"
    elif data_type == "JSON":
        return "string"
    else:
        return "string"
